//
//  WmcAlbumTransitionProtocol.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/29.
//

import UIKit
import YPBedrock

// MARK: - [WmcAlbumTransitionProtocol]
/// 转场动画协议
protocol WmcAlbumTransitionProtocol {}

private var ktransitionImageView = "ktransitionImageView"

extension WmcAlbumTransitionProtocol where Self: WmcBaseVC {
    
    func transitionAnimation(startView: UIView,
                             image: UIImage,
                             endFrame: CGRect,
                             transitionBlock: @escaping YPBlock.Empty) {
        if transitionImageView.superview == nil {
            view.addSubview(transitionImageView)
        }
        view.bringSubviewToFront(transitionImageView) // 前置
        // 转场动画
        let vcRect = startView.convert(startView.bounds, to: view)
        transitionImageView.image = image
        transitionImageView.frame = vcRect
        transitionImageView.isHidden = false
        UIView.animate(withDuration: 0.2,
                       delay: 0,
                       options: .curveEaseOut) { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.transitionImageView.frame = endFrame
        } completion: { _ in
            YPGCD.Delay(delay: 0.4) { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.transitionImageView.isHidden = true
            }
        }
        // 跳转编辑页面
        YPGCD.Delay(delay: 0.08) {
            transitionBlock()
        }
    }
    
    /// 转场ImageView
    var transitionImageView: UIImageView {
        set { YPRuntime.Set(object: self, key: &ktransitionImageView, value:  newValue, policy:  .OBJC_ASSOCIATION_RETAIN) }
        get {
            guard let imageView: UIImageView = YPRuntime.GetObject(self, key: &ktransitionImageView) else {
                let imageView: UIImageView = .init()
                imageView.contentMode = .scaleAspectFit
                imageView.isHidden = true
                self.transitionImageView = imageView
                return imageView
            }
            return imageView
        }
    }
}
